from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status  # 保存了所有HTTP响应对象的常量
from django.views import View
from django.http import HttpResponse


class StudentView(View):
    def get(self, request):  # django提供的view视图，在视图方法中传入的request变量是WSGIHttpRequest
        print(f"request:{request}")  # WSGIRequest-->父类-->django.html.request.HttpRequest
        return HttpResponse("ok")


class StudentAPIView(APIView):
    def get(self, request):
        print(
            f"drf_request:{request}")  # rest_framework.request.Request属于drf单独声明的请求处理对象，与django提供的HTTPRequest不是同一个没有任何继承关系
        print(f"django_request:{request._request}")  # 拿到django的request

        # 1、获取请求体数据

        return Response({"msg": "ok"})

    def post(self, request):
        """添加数据"""
        print(f"request.data:{request.data}")  # 接收数据，已经被parse解析器转换成字典数据

        # 客户端提交json数据
        # {'name': 'xiaodeng', 'sex': True, 'age': 12}

        # 客户端提交表单数据
        # {'name': ['sjdhdh'], 'password': ['123456']}
        # print(request.data.get("name"))

        # 获取查询参数/查询字符串
        print(f"request.query_params{request.query_params}")
        # print(request.query_params.get("class"))
        response = Response({"msg": "ok"}, status=status.HTTP_201_CREATED, headers={"Company": "text/html"})
        return response

    def put(self, request):
        """更新数据"""
        return Response({"msg": "ok"})

    def patch(self, request):
        """更新部分数据"""
        return Response({"msg": "ok"})

    def delete(self, request):
        """删除数据"""
        return Response({"msg": "ok"})
